319B - Psychos in a Line - CodeForces Solution


data structures implementation *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int  mod = 1000000007; 

/*=================================Debug==================================*/
template <typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream& os, const T_container& v) { os << '{'; string sep; for (const T& x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << " " << H; dbg_out(T...); }
 
//#define MY
#ifdef MY
#define debug(args...) cerr << "(" << #args << "):", dbg_out(args)
#else
#define debug(args...)
#endif
int tree[4000009];
void update(int node, int l , int r, int idx, int step ){
    if(l>idx or r<idx)return ; 
    if(l==r){
        tree[node]= step ; 
        return ; 
    }
    int mid =l+r>>1;
    update(node<<1, l, mid, idx, step); 
    update((node<<1)|1, mid+1, r , idx, step); 
    tree[node]=max(tree[node*2], tree[node*2+1]);
}
int query(int node, int l , int r , int st, int ed ){
    debug(node);
    if(l>ed or r<st)return 0; 
    if(l>=st and r<=ed){
        return tree[node];
    }
    return max(query(node<<1, l, (l+r>>1), st, ed), query((node<<1)|1, ((l+r>>1)+1),r, st, ed));
}
int main() {
    //return 0;
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n; 
    cin>>n; 
    vector<int > v; 
    for(int i =1;i <=n;i++){
        int x; 
        cin>>x; 
        v.push_back(x); 
    }
    int ans =0;
    stack<int> s; 
    s.push(0);
    for(int i=1;i<n; i++){
        while( not s.empty() and v[s.top()]<v[i]){
            s.pop(); 
        }
        debug(s.top());
        int step=-1 ; 
        if(not s.empty()){
            step= query(1, 0, n, s.top()+1, i)+1; 
            debug(step); 
        }
        ans =max(ans, step);
        update(1, 0, n, i, step); 
        s.push(i);
    }
    cout<<ans<<endl; 
    return 0;
}
		 		 		  	 			 	   			 	  				


Comments

Submit
0 Comments
More Questions

60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person